/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ensamble;


/**
 *
 * @author JC
 */
public class Conversores{
    
    Bin bin;
    Hex hex [];
    
    public int toBin (int ascii){
                       
        bin = new Bin();
        int bin_checksum = 0;
        int current_bin = 0;        
        int current_stack = 0;
        String bin_frame = "";
        boolean end_of_frame = false;
        
        do{
            
            if (bin_checksum > ascii){
                
                current_stack++;
                continue;
                
            }
            
            if ((bin_checksum + bin.stack[current_stack]) <= ascii){
                
                bin.stacked[current_stack] = true;
                bin_checksum += bin.stack[current_stack];
                
            }
            
            if (current_stack == bin.stack.length -1){
                
                end_of_frame = true;
                
            }else{
                
                end_of_frame = false;
                
            }
            
            current_stack++;
                        
        }while (end_of_frame != true);
        
        current_stack = 0;
        
        do{
            
            if (bin.stacked[current_stack] == false){
                
                bin_frame += "0";
                
            }else{
                
                bin_frame += "1";
                
            }
            
            current_stack++;
            
        }while (current_stack < bin.stacked.length);
        
        current_bin = Integer.parseInt(bin_frame);
        
        return current_bin;
        
    }
    
    public String fromBin (int prev){
        
        String bin_frame = String.valueOf(prev);        
        String current = "";
        
        int frames_number = (bin_frame.length() <= 4) ? 1 : 2;
        int frames_init = frames_number - 1;
        int split_iteration = 0;
        int current_frame = 0;        
                
        hex = new Hex[frames_number];
        String [] frames = new String[frames_number];
        
        while (frames_init >= 0){
            
            frames[frames_init] = "";
            hex[frames_init] = new Hex();
            frames_init--;
            
        }               
                
        for (int bin_position = bin_frame.length() - 1; bin_position >= 0; bin_position--){
            
            frames[current_frame] += bin_frame.charAt(bin_position);              
            split_iteration++;
            hex[current_frame].stackable[hex[current_frame].stack.length - split_iteration] = (bin_frame.charAt(bin_position) == '1') ? true : false;            
                                               
            if (split_iteration == 4){

                split_iteration = 0;
                current_frame++;

            }
                                                        
        }                
                
        current_frame = frames_number - 1;
        
        while (current_frame >= 0){
                                    
            for (int current_stack = hex[current_frame].stackable.length -1; current_stack >= 0  ; current_stack--){
                                
                hex[current_frame].stacked += (hex[current_frame].stackable[current_stack] == true) ? hex[current_frame].stack[current_stack] : 0;
                
            }
            
            if (hex[current_frame].stacked >= 0 && hex[current_frame].stacked < 10){
                
                current += String.valueOf(hex[current_frame].stacked);
                
            }else if (hex[current_frame].stacked == 10){
                
                current += "A";
                
            }else if (hex[current_frame].stacked == 11){
                
                current += "B";
                
            }else if (hex[current_frame].stacked == 12){
                
                current += "C";
                
            }else if (hex[current_frame].stacked == 13){
                
                current += "D";
                
            }else if (hex[current_frame].stacked == 14){
                
                current += "E";
                
            }else if (hex[current_frame].stacked == 15){
                
                current += "F";
                
            }                                                                                                   
            
            current_frame--;                    
        
        }
        
        return current;
        
    }
    
    public int toDec(String hex_frame){
        
        int dec = 0;
        int current_frame = hex_frame.length() - 1;
        hex =  new Hex[hex_frame.length()];        
        int frames_init = current_frame;
        bin = new Bin();
        int current_stack = 0;
        String decimal_bootstrap = "";
        String [] frames = new String[hex_frame.length()];        
        
        do{
            
            hex[frames_init] = new Hex();
            frames[frames_init] = "";
            frames_init--;
            
        }while (frames_init >= 0);
        
        for (int bin_position = hex_frame.length() - 1; bin_position >= 0; bin_position--){
            
            frames[current_frame] += hex_frame.charAt(bin_position);  
            
            int stack = 0;
            
            try{
                
                stack += Integer.parseInt(frames[current_frame]);
                
            }catch(NumberFormatException ex){
                
                if (frames[current_frame].equals("A")){
                    stack += 10;
                }else if (frames[current_frame].equals("B")){
                    stack += 11;
                }else if (frames[current_frame].equals("C")){
                    stack += 12;
                }else if (frames[current_frame].equals("D")){
                    stack += 13;
                }else if (frames[current_frame].equals("E")){
                    stack += 14;
                }else if (frames[current_frame].equals("F")){
                    stack += 15;
                }
                
            }finally{
                
                hex[current_frame].stacked = stack;
                current_stack = toBin(stack);
                decimal_bootstrap = String.valueOf(current_stack) + decimal_bootstrap;
                
            }
            
            current_frame--;//got 8C
                                                        
        }
        
        while (decimal_bootstrap.length() < 8){
            
            decimal_bootstrap =  "0" + decimal_bootstrap;
            
        }
        
        int frame_position = decimal_bootstrap.length() - 1;        
        
        do{
            
            dec += (decimal_bootstrap.charAt(frame_position) == '1') ?  bin.stack[frame_position] : 0;
            frame_position--;
                        
            
        }while(frame_position >= 0);
        //there was code here
        
        return dec;
        
    }
    
}

class Bin{
    
    int stack [] = {128,64,32,16,8,4,2,1};
    boolean stacked [] = {false, false, false, false, false, false, false, false};
    
}

class Hex{
    
    int stack [] = {8,4,2,1};
    boolean stackable [] = {false, false, false, false};
    int stacked = 0;
    
}